// pages/news/newsDetails/newsDetails.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    info: {},
    info2: [],
    msg: '',
    title: '',
    time: '',
    bottom: 0,
    comment: false,
    bigImg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
      this.getNewInfo();
      this.getInfo();
    }
    if (options.comment === 'comment') {
      this.setData({
        comment: true
      });
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // 分享
  onShareAppMessage(res) {
    return {
      title: '广州大学校友会校友企业家专委会',
      path: '/pages/index/index'
    }
  },
  getNewInfo: function() {
    let _this = this;
    app.util.ajax({
      url: "con/info",
      data: {
        id: _this.data.id,
        user_id: app.globalData.userInfo.user_id
      },
      success: function(res) {
        res.content = res.content.replace(/<img/g, '<img style="width:100%;"');
        _this.setData({
          info: res
        });
        if (_this.data.comment) {
          // 滚定高度
          wx.createSelectorQuery().select('#height').fields({
            size: true,
          }, function(res) {
            wx.pageScrollTo({
              scrollTop: res.height,
              duration: 0
            })
          }).exec()
        };
      }
    })
  },
  // 是否点赞
  isPraise: function(e) {
    let state = e.currentTarget.dataset.state ? 0 : 1,
      _this = this;
    app.util.ajax({
      url: 'con/zambia',
      data: {
        id: _this.data.id,
        user_id: app.globalData.userInfo.user_id,
        state: state
      },
      success: function(data) {
        _this.setData({
          comment: false
        });
        _this.getNewInfo();
      }
    })
  },
  getInfo: function() {
    let _this = this;
    app.util.ajax({
      url: "Comment/index",
      data: {
        id: _this.data.id,
        type: 3
      },
      success: function(res) {
        let array = [];
        for (var i in res.data) {
          if (typeof res.data[i] === 'object' && res.data[i] !== null) {
            array.push(res.data[i]);
          };
        };
        _this.setData({
          info2: array,
          title: res.data.title,
          time: res.data.add_time
        })
      }
    })
  },
  getMsg: function(e) {
    this.setData({
      msg: e.detail.value
    })
  },
  send: function() {
    let _this = this;
    if (_this.data.msg === '') {
      wx.showToast({
        title: '请输入评论',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.util.ajax({
        url: "Comment/add",
        method: 'post',
        data: {
          info: {
            id: _this.data.id,
            user_id: app.globalData.userInfo.user_id,
            content: _this.data.msg,
            type: 3
          }
        },
        success: function(res) {
          if (res.state === 1) {
            wx.showToast({
              title: res.info,
              icon: 'success',
              duration: 2000
            })
            _this.getInfo();
            wx.showToast({
              title: res.info,
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.info,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    };
  },
  foucus: function(e) {
    var _this = this;
    _this.setData({
      bottom: e.detail.height
    })
  },
  blur: function(e) {
    var _this = this;
    _this.setData({
      bottom: 0
    })
  },
  showBigImg: function(e) {
    this.setData({
      bigImg: e.currentTarget.dataset.src
    })
  },
  hideBigimg() {
    this.setData({
      bigImg: ''
    })
  }
})